package dev.tools;

import static org.junit.Assert.assertTrue;

import java.io.*;
import java.util.*;

import org.junit.*;

import com.megadict.format.dict.index.Index;
import com.megadict.format.dict.parser.IndexParser;
import com.megadict.format.dict.parser.IndexParsers;

public class TextToNumberConverterTest {

    @Ignore
    public void testToInt() {
        String[] testStrings = { "abbey", "perplexed", "zygospore", "zymology",
                "zymometer", "zoography", "yucky", "yid",
                "fundamental wavelength", "Function income distribution",
                "full-length", "bipart", "biometrically", "biometrician",
                "remonstrating", "zoo" };

        Set<Integer> integers = new TreeSet<Integer>();

        for (int item = 0; item < testStrings.length; item++) {
            int holder = TextToNumberConverter.toInt(testStrings[item]);
            integers.add(holder);
        }
        
        String testString = "zoo";
        int testInt = TextToNumberConverter.toInt(testString);
        
        boolean contained = integers.contains(testInt);
        
        assertTrue(contained);
        
    }

    @Ignore
    public void convertAllIndexStringToInt() {
        IndexFileLoader loader = new IndexFileLoader("C:\\test\\smallIndex.index");

        IndexParser parser = IndexParsers.newParser();
        
        List<String> allIndexString = loader.load();
        
        List<Index> allIndexes = new ArrayList<Index>(allIndexString.size());
        
        for (String index : allIndexString) {
            Index parsed = parser.parse(index);
            allIndexes.add(parsed);
        }

        BufferedOutputStream writer = null;
        try {
            writer = new BufferedOutputStream(new DataOutputStream(new FileOutputStream(
                    "C:\\test\\indexInInt.txt")));
        } catch (FileNotFoundException fnf) {
            fnf.printStackTrace();
        }

        try {

            writer.write(allIndexes.size());

            for (Index index : allIndexes) {
                int number = TextToNumberConverter.toInt(index.getWord());
                writer.write(number);
            }
        } catch (IOException ioe) {
            ioe.printStackTrace();
        } finally {
            try {
                writer.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }

    @Test
    public void testLoadAllNumbersToSetAndTryFindingWord() {

        BufferedInputStream reader = null;

        try {
            FileInputStream fileInputStream = new FileInputStream(
                    "C:\\test\\indexInInt.txt");
            reader = new BufferedInputStream(new DataInputStream(fileInputStream));
        } catch (FileNotFoundException fnf) {
            fnf.printStackTrace();
            return;
        }

        Set<Integer> numbers = new TreeSet<Integer>();

        try {
            int totalIndexes = reader.read();

            for (int count = 0; count < totalIndexes; count++) {
                int holder = reader.read();
                numbers.add(Integer.valueOf(holder));
            }
        } catch (IOException ioe) {
            ioe.printStackTrace();
        } finally {
            try {
                reader.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        
        String testString = "00-database-info";

        int stringToNumber = TextToNumberConverter.toInt(testString);

        boolean contained = numbers.contains(stringToNumber);

        assertTrue(contained);

    }

}
